package com.sg.service.base.framework.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sg.common.util.CommonFunctionHelper;
import com.sg.dto.base.framework.req.QuerySoftwareAuthorizationDetailReq;
import com.sg.dto.base.framework.req.QuerySoftwareAuthorizationListReq;
import com.sg.entity.OmsSoftwareAuthorization;
import com.sg.service.base.framework.MOmsSoftwareAuthorizationService;
import com.wicket.okrframework.common.dto.FieldInfo;
import com.wicket.okrframework.common.exception.BizException;
import com.wicket.okrframework.dal.mapper.mbg.OmsSoftwareAuthorizationMapper;
import org.apache.skywalking.apm.toolkit.trace.Tag;
import org.apache.skywalking.apm.toolkit.trace.Tags;
import org.apache.skywalking.apm.toolkit.trace.Trace;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @author ：
 * @version 1.0
 * @since 2022/1/19 15:39
 */
@Service
public class MOmsSoftwareAuthorizationServiceImpl
        extends ServiceImpl<OmsSoftwareAuthorizationMapper, OmsSoftwareAuthorization>
        implements MOmsSoftwareAuthorizationService {

    @Autowired
    OmsSoftwareAuthorizationMapper omsSoftwareAuthorizationMapper;

    /**
     * 1-14-01查询软件授权证书列表[6552]
     * gen by moon at 5/13/2023, 9:07:13 PM
     */
    @Trace(operationName = "1-14-01查询软件授权证书列表")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    @Override
    @Transactional(rollbackFor = Exception.class)
    public List<OmsSoftwareAuthorization> querySoftwareAuthorizationList(QuerySoftwareAuthorizationListReq reqDto) {

        try {
            QueryWrapper<OmsSoftwareAuthorization> queryWrapper = new QueryWrapper<>();
            reqDto.setIsValid("FALSE");

            Map<String, Object> fieldInfoMap = null;
            fieldInfoMap = CommonFunctionHelper.objectToMapForLamBda(reqDto);
            List<FieldInfo> compareList = CommonFunctionHelper.getRangeField(OmsSoftwareAuthorization.class);
            CommonFunctionHelper.buildQueryWrapperCompare(queryWrapper, compareList, fieldInfoMap);
            List<FieldInfo> likeList = CommonFunctionHelper.getLikeFiled(OmsSoftwareAuthorization.class);
            CommonFunctionHelper.buildQueryWrapperLike(queryWrapper, likeList, fieldInfoMap);
            likeList.addAll(compareList);
            CommonFunctionHelper.buildQueryWrapperEq(queryWrapper, fieldInfoMap, likeList);
            List<FieldInfo> orderList = CommonFunctionHelper.getOrderList(OmsSoftwareAuthorization.class);
            //CommonFunctionHelper.buildQueryWrapperOrder(queryWrapper, orderList);

            return ((List<OmsSoftwareAuthorization>) super.list(queryWrapper));
        } catch (Exception e) {
            e.printStackTrace();
            throw new BizException("-1", e, false);
        }
    }

    /**
     * 1-04-01查询软件授权证书详情[6605]
     * gen by moon at 5/12/2023, 2:45:35 AM
     */
    @Trace(operationName = "1-04-01查询软件授权证书详情")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    @Override
    @Transactional(rollbackFor = Exception.class)
    public OmsSoftwareAuthorization querySoftwareAuthorizationDetail(QuerySoftwareAuthorizationDetailReq reqDto) {

        try {
            reqDto.setIsValid("FALSE");
            QueryWrapper<OmsSoftwareAuthorization> queryWrapper = new QueryWrapper<>();
            Map<String, Object> fieldInfoMap = null;
            fieldInfoMap = CommonFunctionHelper.objectToMapForLamBda(reqDto);
            CommonFunctionHelper.buildQueryWrapperEq(queryWrapper, fieldInfoMap, new ArrayList<>());
            queryWrapper.orderByDesc("create_time");
            List<OmsSoftwareAuthorization> retList = super.list(queryWrapper);
            if (retList != null && retList.size() > 0) {
                OmsSoftwareAuthorization retData = retList.get(0);

                return retData;
            }
            return null;
        } catch (Exception e) {
            e.printStackTrace();
            throw new BizException("-1", e, false);
        }
    }

    /**
     * 1-04-01新增软件授权证书[6607]
     * gen by moon at 5/12/2023, 2:45:36 AM
     */
    @Trace(operationName = "1-04-01新增软件授权证书")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    @Override
    @Transactional(rollbackFor = Exception.class)
    public String addSoftwareAuthorization(OmsSoftwareAuthorization omsSoftwareAuthorization) {
        omsSoftwareAuthorization.setAuthorizationId(CommonFunctionHelper.getUid());//DISTRIBUTED_ID
        omsSoftwareAuthorization.setIsArchive("FALSE");
        omsSoftwareAuthorization.setOperationInductionId(CommonFunctionHelper.getBaseInfoFromLocal().getSpecInductionRecordId());//CURRENT_INDUCTIONID
        omsSoftwareAuthorization.setCreateInductionId(CommonFunctionHelper.getBaseInfoFromLocal().getSpecInductionRecordId());//CURRENT_INDUCTIONID
        if (omsSoftwareAuthorization != null && omsSoftwareAuthorization.getSubjectLifeCycle() != null && omsSoftwareAuthorization.getSubjectLifeCycle().equals("CURRENTLY_VALID")) {
            omsSoftwareAuthorization.setReleaseInductionId(CommonFunctionHelper.getBaseInfoFromLocal().getSpecInductionRecordId());//RELEASED_CURRENT_INDUCTIONID
        }
        if (omsSoftwareAuthorization != null && omsSoftwareAuthorization.getSubjectLifeCycle() != null && omsSoftwareAuthorization.getSubjectLifeCycle().equals("ARCHIVING")) {
            omsSoftwareAuthorization.setArchiveInductionId(CommonFunctionHelper.getBaseInfoFromLocal().getSpecInductionRecordId());//ARCHIVE_CURRENT_INDUCTIONID
        }
        omsSoftwareAuthorization.setOperateTime(CommonFunctionHelper.getCurrentTime());//SYSTEM_CURRENT_TIME
        omsSoftwareAuthorization.setCreateTime(CommonFunctionHelper.getCurrentTime());//SYSTEM_CURRENT_TIME
        if (omsSoftwareAuthorization != null && omsSoftwareAuthorization.getSubjectLifeCycle() != null && omsSoftwareAuthorization.getSubjectLifeCycle().equals("CURRENTLY_VALID")) {
            omsSoftwareAuthorization.setReleaseTime(CommonFunctionHelper.getCurrentTime());//RELEASE_SYSTEM_CURRENT_TIME
        }
        if (omsSoftwareAuthorization != null && omsSoftwareAuthorization.getSubjectLifeCycle() != null && omsSoftwareAuthorization.getSubjectLifeCycle().equals("ARCHIVING")) {
            omsSoftwareAuthorization.setArchiveTime(CommonFunctionHelper.getCurrentTime());//HISTORICAL_ARCHIVING
        }
        omsSoftwareAuthorization.setIsValid("FALSE");
        omsSoftwareAuthorization.setSpaceId(CommonFunctionHelper.getBaseInfoFromLocal().getSpecSpaceId());//CURRENT_SPACEID
        omsSoftwareAuthorization.setAppId(CommonFunctionHelper.getBaseInfoFromLocal().getSpecAppId());//CURRENT_APPID
        if (super.save(omsSoftwareAuthorization)) {
            return omsSoftwareAuthorization.getAuthorizationId();
        } else {
            return "-1";
        }

    }

    /**
     * 1-04-01修改软件授权证书[6612]
     * gen by moon at 5/12/2023, 2:45:37 AM
     */
    @Trace(operationName = "1-04-01修改软件授权证书")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean updateSoftwareAuthorization(OmsSoftwareAuthorization omsSoftwareAuthorization) {
        omsSoftwareAuthorization.setOperationInductionId(CommonFunctionHelper.getBaseInfoFromLocal().getSpecInductionRecordId());//CURRENT_INDUCTIONID
        if (omsSoftwareAuthorization != null && omsSoftwareAuthorization.getSubjectLifeCycle() != null && omsSoftwareAuthorization.getSubjectLifeCycle().equals("ARCHIVING")) {
            omsSoftwareAuthorization.setArchiveInductionId(CommonFunctionHelper.getBaseInfoFromLocal().getSpecInductionRecordId());//ARCHIVE_CURRENT_INDUCTIONID
        }
        omsSoftwareAuthorization.setOperateTime(CommonFunctionHelper.getCurrentTime());//SYSTEM_CURRENT_TIME
        if (omsSoftwareAuthorization != null && omsSoftwareAuthorization.getSubjectLifeCycle() != null && omsSoftwareAuthorization.getSubjectLifeCycle().equals("ARCHIVING")) {
            omsSoftwareAuthorization.setArchiveTime(CommonFunctionHelper.getCurrentTime());//HISTORICAL_ARCHIVING
        }
        return super.updateById(omsSoftwareAuthorization);
    }

    /**
     * 1-04-01删除软件授权证书[6615]
     * gen by moon at 5/12/2023, 2:45:37 AM
     */
    @Trace(operationName = "1-04-01删除软件授权证书")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean deleteSoftwareAuthorization(String authorizationId) {

        return super.removeById(authorizationId);
    }
}
